using UnityEngine;
using UnityEngine.UI;

namespace Yoozoo.Utils
{
    //用好lua+unity，让性能飞起来——lua与c#交互篇
    //https://www.cnblogs.com/zwywilliam/p/5999924.html
    
    public class LuaUtils
    {
        public static bool IsNull(GameObject obj)
        {
            return obj == null;
        }
        public static void SetPosition(GameObject obj,float x,float y,float z)
        {
            if (obj == null)
            {
                return;
            }
            obj.transform.position = new Vector3(x,y,z);
        }

        public static void SetLocalPosition(GameObject obj,float x,float y,float z)
        {
            if (obj == null)
            {
                return;
            }
            obj.transform.localPosition = new Vector3(x,y,z);
        }

        public static void FindTransformAndSetActive(GameObject obj,string tag,bool isActive)
        {
            var trans = obj.transform.Find(tag);
            if (trans)
            {
                trans.SetActive(isActive);
            }
        }
        public static void SetAnchoredPosition(GameObject obj, float x, float y)
        {
            if (obj == null)
                return;

            SetAnchoredPosition(obj.GetComponent<RectTransform>(), x, y);
        }

        public static void SetAnchoredPositionX(GameObject obj, float x)
        {
            if (obj == null)
                return;

            var rt = obj.GetComponent<RectTransform>();
            SetAnchoredPosition(rt, x, rt.anchoredPosition.y);
        }
        
        public static void SetAnchoredPositionY(GameObject obj, float y)
        {
            if (obj == null)
                return;

            var rt = obj.GetComponent<RectTransform>();
            SetAnchoredPosition(rt, rt.anchoredPosition.x, y);
        }

        public static void SetAnchoredPosition(RectTransform rt, float x, float y)
        {
            if (rt == null)
                return;

            var v2 = new Vector2(x, y);
            if (v2 == rt.anchoredPosition)
                return;

            rt.anchoredPosition = v2;
        }

        public static void SetSizeDelta(GameObject obj, float x, float y)
        {
            if (obj == null)
                return;
            
            SetSizeDelta(obj.GetComponent<RectTransform>(), x, y);
        }

        public static void SetSizeDelta(RectTransform rt, float x, float y)
        {
            if (rt == null)
                return;

            var v2 = new Vector2(x, y);
            if (rt.sizeDelta == v2)
                return;

            rt.sizeDelta = v2;
        }
        
        public static void SetSizeDeltaX(GameObject obj, float x)
        {
            if (obj == null)
            {
                return;
            }
            var rectTrans = obj.transform.GetComponent<RectTransform>();

            if (rectTrans)
            {
                rectTrans.sizeDelta = new Vector2(x,rectTrans.sizeDelta.y);
            }
        }
        public static void SetSizeDeltaY(GameObject obj, float y)
        {
            if (obj == null)
            {
                return;
            }
            var rectTrans = obj.transform.GetComponent<RectTransform>();

            if (rectTrans)
            {
                rectTrans.sizeDelta = new Vector2(rectTrans.sizeDelta.x,y);
            }
        }
        public static void SetParent(GameObject obj, GameObject parent, bool worldPositionStays)
        {
            obj.transform.SetParent(parent.transform,worldPositionStays);
        }

        public static float GetWidth(GameObject obj)
        {
            var trans = obj.transform.GetComponent<RectTransform>();
            return trans.sizeDelta.x;
        }

        public static bool SetPadding(LayoutGroup group, int l, int r, int t, int b)
        {
            var old = group.padding;
            var flag = false;
            if (old.left != l)
            {
                old.left = l;
                flag = true;
            }
            
            if (old.right != r)
            {
                old.right = r;
                flag = true;
            }
            
            if (old.top != t)
            {
                old.top = t;
                flag = true;
            }
            
            if (old.bottom != b)
            {
                old.bottom = b;
                flag = true;
            }

            if (!flag)
                return false;

            group.padding = old;
            return true;
        }

        public static Vector3 FindRayTarget(Camera camera)
        {
            if (camera == null)
                return new Vector3(-1, -1, -1);
            
            RaycastHit hitInfo;
            if (Physics.Raycast(camera.ViewportPointToRay(new Vector3(0.5f, 0.5f)), out hitInfo))
                return hitInfo.point;
            
            return new Vector3(-1, -1, -1);
        }
    }
}